<template>
	<view class="content poRel">
		<!-- <view class="wid100" style="position: fixed;top: 0;">
			<view class="hei80 dis disAl wid90 mar">
				<view style="border-left: 6rpx solid #1262DB;padding-left: 10rpx;">采购列表</view>
			</view>
			<view class="hei87 dis foSi30 bacFFF">
				<view class="hei100 dis disAl disJuC wid25" @click="xuanze(0)"
					:style="status == 0?'color: #1262DB;':''">全部</view>
				<view class="hei100 dis disAl disJuC wid25" @click="xuanze(1)"
					:style="status == 1?'color: #1262DB;':''">待付款</view>
				<view class="hei100 dis disAl disJuC wid25" @click="xuanze(2)"
					:style="status == 2?'color: #1262DB;':''">待收货</view>
				<view class="hei100 dis disAl disJuC wid25" @click="xuanze(3)"
					:style="status == 3?'color: #1262DB;':''">已完成</view>
			</view>
		</view>
		<view style="height: 167rpx;"></view>
		<scroll-view style="height: calc(100vh - 167rpx);" scroll-y="true" @scrolltolower="lower">
			<view v-for="(item,index) in list" :key="index" class="wid100 bacFFF mar-bott30" style="padding: 20rpx 0;">
				<view class="wid90 mar">
					<view class="dis">
						<view class="textCen" style="width: 30%;">
							<image :src="url+item.pro_img" style="width: 138rpx;height: 138rpx;"></image>
						</view>
						<view style="width: 70%;">
							<view class="wid100 dis disAl disJuB hei60 foSi30">
								<view>{{item.pro_name}}</view>
								<view v-if="item.status != 3" style="font-weight: 600;color: #ff0000;">
									{{item.status == 0?'待付款':item.status == 1?'待发货':item.status == 2?'已发货':'已取消'}}
								</view>
								<view v-else style="font-weight: 600;color: #1262DB;">已完成</view>
							</view>
							<view class="mar-top20 foSi25" style="color: #858585;">共{{item.pro_num}}件商品 合计：¥{{item.pay_money}}</view>
							<view class="mar-top20 wid100 dis disAl hei80"
								style="justify-content: flex-end;font-size: 23rpx;">
								<view class="dis disAl disJuC" v-if="item.status < 2" @click="qTrue(index,4)"
									style="border: 1rpx solid #999898;width: 131rpx;height: 44rpx;color: #999898;">取消订单</view>
								<view class="dis disAl disJuC" v-if="item.status > 2 && item.status != 4"
									@click="chakan(index)"
									style="border: 1rpx solid #999898;width: 131rpx;height: 44rpx;color: #999898;">查看物流</view>
								<view class="dis disAl disJuC coFFF mar-left20" v-if="item.status == 0"
									@click="fukuan(index)"
									style="width: 131rpx;height: 44rpx;background-color: #1262DB;">确认付款</view>
								<view class="dis disAl disJuC coFFF mar-left20"
									v-if="item.status > 0 && item.status < 2" @click="qTrue(index,3)"
									style="width: 131rpx;height: 44rpx;background-color: #1262DB;">确认收货</view>
								<view class="dis disAl disJuC coFFF mar-left20" v-if="item.status > 2"
									 @click="tiaozhuan('/pages/purchase/confirmAnOrder?prolist='+JSON.stringify(item.buyagain))"
									style="width: 131rpx;height: 44rpx;background-color: #1262DB;">再次购买</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view v-if="list.length != 0" style="height: 50rpx;"></view>
			<view v-if="list.length == 0" class="wid100 dis disAl disJuC" style="height: calc(100vh - 217rpx);">
				<image src="../../static/incomeDetail/wushiju.png" style="width: 197rpx;height: 226rpx;"></image>
			</view>
		</scroll-view> -->
	</view>
</template>

<script>
	var md5 = require('../../components/md5.js')
	export default {
		data() {
			return {
				url: this.global.url,
				status: 0,
				last_page: 0,
				page: 0,
				list: []
			}
		},
		onLoad(options) {
			this.myorder()
		},
		methods: {
			tiaozhuan(url) {
				uni.navigateTo({
					url: url
				});
			},
			lower: function(e) {
				if (this.last_page != this.page + 1) {
					this.page++
					this.myorder()
				}
			},
			xuanze(index) {
				this.status = index
				this.page = 0
				this.list = []
				this.myorder()
			},
			chakan(index) {
				uni.showModal({
					title: this.list[index].delivery,
					content: "单号："+this.list[index].delivery_num,
					success(res) {
						if(res.confirm){
							console.log("确定")
						}else if(res.cancel){
							console.log("取消")
						}
					}
				})
			},
			fukuan(index) {
				var that = this
				uni.request({
					url: that.global.url + 'api/proorder/confirm_pay',
					method: "POST",
					header: {
						'token': uni.getStorageSync('token')
					},
					data: {
						id: this.list[index].id
					},
					success(res) {
						let result = res.data
						if (result.code == 1) {
							console.log(result.data)
							let pay_key = result.data.pay_key
							console.log(pay_key)
							// return false;
							//obj里面的key一定要是驼峰名称
							let obj = {
								appId: result.data.pyainfo.appid,
								nonceStr: result.data.pyainfo.noncestr,
								package: "prepay_id=" + result.data.pyainfo.package,
								signType: 'MD5',
								timeStamp: result.data.pyainfo.timestamp
							}
							//参数名ASCII码从小到大排序（字典序）
							let arr = Object.keys(obj).sort().map(item => {
								return `${item}=${obj[item]}`;
							});
							//最后拼接上key（商户密钥）得到字符串
							let str = arr.join('&') + '&key=' + pay_key;
							console.log(str);
							//对str进行MD5运算，再将得到的字符串所有字符转换为大写
							console.log(result.data.pyainfo.timestamp)
							let paySign = md5.hexMD5(str).toUpperCase()
							wx.requestPayment({
								timeStamp: result.data.pyainfo.timestamp.toString(),
								nonceStr: result.data.pyainfo.noncestr,
								package: "prepay_id=" + result.data.pyainfo.package,
								signType: 'MD5',
								paySign: paySign,
								success: function(res) {
									// 支付成功
									uni.showModal({
										title: '提示',
										content: '支付成功',
										showCancel: false,
										success: function(res) {
											if (res.confirm) {
												uni.navigateBack({
													delta: 2
												})
											}
										}
									})
								},
								fail: function(res) {
									// 支付失败
									console.log(res)
									uni.showToast({
										title: '支付失败',
										icon: 'none'
									})
								}
							})
						} else {
							uni.showToast({
								title: result.msg,
								icon: 'error'
							})
						}

					}
				})
			},
			qTrue(index, i) {
				var that = this
				uni.request({
					url: that.global.url + 'api/proorder/cance_order',
					method: "POST",
					header: {
						'token': uni.getStorageSync('token')
					},
					data: {
						id: this.list[index].id,
						status: i
					},
					success(res) {
						let result = res.data
						if (result.code == 1) {
							uni.showToast({
								title: '订单修改成功',
							})
							that.page = 0
							that.myorder()
						} else {
							uni.showToast({
								title: result.msg,
								icon: 'error'
							})
						}

					}
				})
			},
			myorder() {
				var that = this
				uni.request({
					url: that.global.url + 'api/proorder/myorder',
					method: "POST",
					header: {
						'token': uni.getStorageSync('token')
					},
					data: {
						status: this.status == 0 ? 5 : this.status - 1
					},
					success(res) {
						let result = res.data
						if (result.code == 1) {
							that.last_page = result.data.last_page
							that.list = result.data.data
						} else {
							uni.showToast({
								title: result.msg,
								icon: 'error'
							})
						}

					}
				})
			}
		}
	}
</script>

<style>
	.content {
		height: 100vh;
		background-color: #F9F9F9;
	}

	.wid25 {
		width: 25%;
	}

	::-webkit-scrollbar {
		display: none;
	}
</style>